package com.example.pageexample.net;

import android.util.Log;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class OperationRequest {
    private static final String TAG = "OperationRequest";
    private final static String baseUrl = "https://image.baidu.com/search/";

    //基于关键词搜索图片
    public void getPicture(String keyWords){
        Retrofit retrofit = getEntity();
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<Object> call = retrofitService.getPicture(keyWords);
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Log.d(TAG,"response body = "+response.body());
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Log.d(TAG,"error");
            }
        });
    }

    //构建请求实体
    private Retrofit getEntity(){
        PictureInterceptor interceptor = new PictureInterceptor();
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        return retrofit;
    }
}
